package com.leo.util.tcpclient;


import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
	import java.io.InputStream;
	import java.io.OutputStream;
import java.io.RandomAccessFile;
	import java.net.Socket;
	import java.nio.ByteBuffer;
	import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

	public class DownloadFileClient {

		public static void main(String[] args) {
			int numTasks = 1;

			ExecutorService exec = Executors.newCachedThreadPool();
			for (int i = 0; i < numTasks; i++) {
				exec.execute(createTask(i));
			}

		}

		// ����һ���򵥵�����
		private static Runnable createTask(final int taskID) {
			
			return new Runnable() {
				private Socket socket = null;

				public void run() {
					try {
						System.out.println("Task " + taskID + ":start");
						long startTime = System.currentTimeMillis();   //��ȡ��ʼʱ��
						//String ip = "106.3.226.140";
						//String ip = "127.0.0.1";
						String ip = "192.168.1.177";
						socket = new Socket(ip, 10091);
						//socket.setSoTimeout(5000);
						// ���͹ر�����
						System.out.println("IP ---------" + socket.getLocalSocketAddress());
						OutputStream socketOut = socket.getOutputStream();
						
						ByteBuffer byteBuffer = ByteBuffer.allocate(205);

						ByteBuffer binVer = ByteBuffer.allocate(4);
						binVer.put("v301".getBytes());

						ByteBuffer custId = ByteBuffer.allocate(32);
						custId.put("C00201312121445".getBytes());

						ByteBuffer configNo = ByteBuffer.allocate(16);
						configNo.put("A13122607110021".getBytes());

						ByteBuffer fileNo = ByteBuffer.allocate(16);
						fileNo.put("A13122607110021".getBytes());

						ByteBuffer androidId = ByteBuffer.allocate(32);
						androidId.put("888888888888888".getBytes());

						ByteBuffer imsi = ByteBuffer.allocate(32);
						imsi.put("460020000000000".getBytes());

						ByteBuffer plat = ByteBuffer.allocate(16);
						plat.put("MT8888".getBytes());

						ByteBuffer defaultStr = ByteBuffer.allocate(16);
						defaultStr.put("11".getBytes());

						ByteBuffer defaultStr2 = ByteBuffer.allocate(16);
						defaultStr2.put("".getBytes());

						ByteBuffer notPross = ByteBuffer.allocate(15);
						notPross.put("".getBytes());
						
						byteBuffer.put(binVer.array());
						byteBuffer.putInt(0);
						byteBuffer.put(custId.array());
						byteBuffer.put(configNo.array());
						byteBuffer.put(fileNo.array());
						byteBuffer.put(androidId.array());
						byteBuffer.put(imsi.array());
						byteBuffer.put(plat.array());
						byteBuffer.put(defaultStr.array());
						byteBuffer.put(defaultStr2.array());
						byteBuffer.putInt(Integer.reverseBytes(17));
						byteBuffer.put(notPross.array());
						byteBuffer.rewind();
						socketOut.write(byteBuffer.array());
						socketOut.flush();
						
						///Thread.sleep(6000);
						// ���շ������ķ���
						InputStream inputStream = socket.getInputStream();
						
						int total = 152;
						byte[] rea = new byte[total]; // �ͻ�������ͷ
						int recvCount = 0; // �Ѿ��ɹ���ȡ���ֽڵĸ���
						
						// ѭ��������������ݣ�ֱ������λ��
						while (recvCount < total) {
							recvCount += inputStream.read(rea, recvCount, total-recvCount);
						}
						//System.out.println(new String(rea));
						
						int baoliu = 0;
						int fileLength = 0;
						int recvLength = 0;
						int isLock = 0;
						String confNo = "";
						String netNum = "";
						String wifi = "";
						String configContext = "";
						
						ByteBuffer buffer = ByteBuffer.allocate(4);
						buffer.put(rea, 0, 4);
						buffer.rewind();
						baoliu = Integer.reverseBytes(buffer.getInt());
						buffer.flip();
						
						ByteBuffer buffer2 = ByteBuffer.allocate(4);
						buffer2.put(rea, 4, 4);
						buffer2.rewind();
						fileLength = Integer.reverseBytes(buffer2.getInt());
						buffer2.flip();
						
						ByteBuffer buffer3 = ByteBuffer.allocate(4);
						buffer3.put(rea, 8, 4);
						buffer3.rewind();
						recvLength = Integer.reverseBytes(buffer3.getInt());
						buffer2.flip();
						
						ByteBuffer buffer4 = ByteBuffer.allocate(4);
						buffer4.put(rea, 12, 4);
						buffer4.rewind();
						isLock = Integer.reverseBytes(buffer4.getInt());
						buffer4.flip();
						
						confNo = new String(rea, 16, 16).trim();
						if(null == confNo || "".equals(confNo)){
							confNo = "C00000000001";
						}
						netNum = new String(rea, 32, 4).trim();
						//netNum = new String(rea, 32, 120).trim();
						wifi = new String(rea, 36, 4).trim();
						File file = new File("d:/2013.apk");

						 BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
						
						if(recvLength != 0){
							byte[] byteArr = new byte[recvLength]; // �ͻ�������ͷ
							int readCount = 0; // �Ѿ��ɹ���ȡ���ֽڵĸ���
							// ѭ��������������ݣ�ֱ������λ��
							while (readCount < recvLength) {
								readCount += inputStream.read(byteArr, readCount, recvLength-readCount);
								System.out.println("��ȡ��" + readCount);
								
							}
							outputStream.write(byteArr, 0, readCount);
							configContext = new String(byteArr, 0, recvLength).trim();
							System.out.println("�ܹ����գ�" + recvLength);
							
						}
						
						System.out.println("-------baoliu:" + baoliu + ", fileLength:" + fileLength + ", recvLength:" +recvLength +"," + " isLock:" +isLock + ", confNo:" + confNo + ",netNum:" + netNum + ",wifi:" + wifi + ", configContext:"+ configContext);
						
						
						
						long endTime = System.currentTimeMillis(); //��ȡ����ʱ��

						System.out.println("��������ʱ�䣺 "+(endTime-startTime)/1000f+"��");
					} catch (IOException ioEx) {
						ioEx.printStackTrace();
						try {
							if (socket != null) {
								socket.close();
							}
						} catch (IOException ioe) {
							// TODO Auto-generated catch block
							ioe.printStackTrace();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						try {
							if (socket != null) {
								socket.close();
							}
						} catch (IOException ioe) {
							// TODO Auto-generated catch block
							ioe.printStackTrace();
						}
					} finally {
						try {
							if (socket != null) {
								socket.close();
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}

			};
	}
}
